﻿using System;
using System.Collections;
using Assets.Script.Data.Player;
using Assets.Script.HelpSystem.AtkSystem.Base;
using Assets.Script.HelpSystem.AtkSystem.Config;
using Assets.Script.Human.Player.Config;
using Assets.Script.NetSystem.Services.HallService;
using Assets.Script.NetSystem.Services.RoomAndBattle.HallService.Handler;
using Script.NetSystem.Core;
using Script.NetSystem.Services.RoomAndBattle.RoomService.Handler;
using Script.UiSystem.Hall;
using Script.UiSystem.Room;
using SimpleTools.Json;
using SimpleTools.Net.SimpleServer.UDP;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Script.NetSystem.Services.RoomAndBattle.RoomService
{
    public class RoomClient : BaseClient<RoomController, IRoomHandler>
    {
        RoomController roomController;
        public RoomClient(int port, string userKey, RoomController roomController,ConnectSuccess onConnectSuccess) : base(port, userKey, onConnectSuccess)
        {
            this.roomController = roomController;
        }
        
        /// <summary>
        /// 加入房间
        /// </summary>
        public void JoinRoom()
        {
            RoleData roleData = new RoleData();
            roleData.name = "asfkg";
            
            roleData.atkInfos = new AtkInfo[] { AtkInfoDic.GetInfo("ShootNormal"), AtkInfoDic.GetInfo("GunS"), AtkInfoDic.GetInfo("Healing") };
            roleData.playerData = new PlayerData
            {
                AttackDir = new AttackDir() {forward = 1, back = 0, left = 0, rigt = 0},
                ShiedRecTime = new BaseData(3),
                MaxHp = new BaseData(100),
                MaxMana = new BaseData(50),
                RunSpeed = new BaseData(4),
                AtkSpeed = new BaseData(2),
                InjuredInterval = new BaseData(0.5f),
                BulletData = new BulletData()
                {
                    Atk = new BaseData(3),
                    BulletScale = new BaseData(1),
                    BulletLifeTime = new BaseData(3),
                    BulletSpeed = new BaseData(10)
                }
            };
        

            JsonData jsonData = new JsonData("uploadrole");
            jsonData.Add("roledata", roleData);
            ClientSend(jsonData);
        }

        /// <summary>
        /// 离开房间
        /// </summary>
        public void LeaveRoom()
        {
            client.Stop();
            HallController.Instance.HallClient = new HallClient(NetConst.Port,"连接大厅", delegate
            {
                HallController.Instance.HallClient.GetRoomsList("Case01");
                SceneManager.LoadScene("Scenes/HallTemp");
            });
        }

        public void ReadyGame(bool ready)
        {
            JsonData jsonData = new JsonData("ready");
            jsonData.Add("isReady", ready);
            ClientSend(jsonData);
        }

        protected override Type[] GetHandlerTypes()
        {
            return new[] {typeof(RoomRoles)};
        }
        
        protected override RoomController GetThisClient()
        {
            return roomController;
        }
    }
}
